<?php
//include 'leftPanel.inc';
//include 'rightPanel.inc';
require_once 'connectvars.php';
$page_title = 'Gửi Thiệp';
include_once 'header.inc';

if (!function_exists('checkdnsrr')) {

    function checkdnsrr($Domain, $Type = 'MX') {
        $Result = FALSE;
        @exec("nslookup -type=$Type $Domain", $Output);
        while ((list($Junk, $Line) = each($Output)) && $Result === FALSE) {
            if (eregi("^$Domain", $Line)) {
                $Result = TRUE;
            }
        }
        return($Result);
    }

}

if (isset($_POST['gui'])) {
    $thiep_id = $_POST['thiepID'];
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $data = mysqli_query($dbc, "SELECT * FROM tbl_thiep WHERE thiep_id = $thiep_id");
    $thiep = mysqli_fetch_array($data);

    $email_gui = $_POST['email_gui'];
    $email_nhan = $_POST['email_nhan'];
    $ten_gui = $_POST['ten_gui'];
    $ten_nhan = $_POST['ten_nhan'];
    $tieu_de = $_POST['tieu_de'];
    $loi_nhan = $_POST['loi_nhan'];
    $output_form = 'no';

    $error_email_gui = "";
    $error_email_nhan = "";
    $error_ten_gui = "";
    $error_ten_nhan = "";
    $error_tieu_de = "";

    if (empty($ten_gui)) {
        // $first_name is blank
        $error_ten_gui = "Ban chua nhap ten nguoi gui.";
        $output_form = 'yes';
    }

    if (empty($ten_nhan)) {
        // $last_name is blank
        $error_ten_nhan = "Ban chua nhap ten nguoi nhan.";
        $output_form = 'yes';
    }

    if (empty($tieu_de)) {
        // $last_name is blank
        $error_tieu_de = "Ban chua nhap tieu de thiep";
        $output_form = 'yes';
    }

    if (!preg_match('/^[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', $email_gui)) {
        // $email is invalid because LocalName is bad
        $error_email_gui = 'Email khong hop le.';
        $output_form = 'yes';
    } else {
        // Strip out everything but the domain from the email
        $domain = preg_replace('/^[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', '', $email_gui);
        // Now check if $domain is registered
        if (!checkdnsrr($domain)) {
            $error_email_gui = 'Email khong hop le.';
            $output_form = 'yes';
        }
    }

    if (!preg_match('/^[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', $email_nhan)) {
        // $email is invalid because LocalName is bad
        $error_email_nhan = 'Email khong hop le.';
        $output_form = 'yes';
    } else {
        // Strip out everything but the domain from the email
        $domain = preg_replace('/^[a-zA-Z0-9][a-zA-Z0-9\._\-&!?=#]*@/', '', $email_nhan);
        // Now check if $domain is registered
        if (!checkdnsrr($domain)) {
            $error_email_nhan = 'Email khong hop le.';
            $output_form = 'yes';
        }
    }
} elseif (isset($_POST['xem_truoc'])) {

    $thiep_id = $_POST['thiepID'];
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $data = mysqli_query($dbc, "SELECT * FROM tbl_thiep WHERE thiep_id = $thiep_id");
    $thiep = mysqli_fetch_array($data);

    $email_gui = $_POST['email_gui'];
    $email_nhan = $_POST['email_nhan'];
    $ten_gui = $_POST['ten_gui'];
    $ten_nhan = $_POST['ten_nhan'];
    $tieu_de = $_POST['tieu_de'];
    $loi_nhan = $_POST['loi_nhan'];

    include 'xemTruoc.php';
    exit();
} else if (isset($_POST['back'])) {

    $thiep_id = $_POST['thiepID'];
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $data = mysqli_query($dbc, "SELECT * FROM tbl_thiep WHERE thiep_id = $thiep_id");
    $thiep = mysqli_fetch_array($data);

    $email_gui = $_POST['email_gui'];
    $email_nhan = $_POST['email_nhan'];
    $ten_gui = $_POST['ten_gui'];
    $ten_nhan = $_POST['ten_nhan'];
    $tieu_de = $_POST['tieu_de'];
    $loi_nhan = $_POST['loi_nhan'];

    $output_form = 'yes';
} else {
    $output_form = 'yes';
    $thiep_id = isset($_GET['thiepID']) ? $_GET['thiepID'] : 1;
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $data = mysqli_query($dbc, "SELECT * FROM tbl_thiep WHERE thiep_id = $thiep_id");
    $thiep = mysqli_fetch_array($data);
}

if ($output_form == 'yes') {

    ?>
    <div id="mainContent">
        <div id="middleContent">
            <div id="pic" align="center">
                <table width="100%" border="0" cellspacing="10" cellpadding="10" >
                    <tr>
                        <td style="text-align: center">
                            <img width="400" border="1" longdesc="#" src="<?php echo $thiep['thiep_hinh']; ?>" alt="certer"/>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="sanpham" class="text">
                <h1>Gửi Thiệp</h1>
                <form action="guiThiep.php" method="POST">
                    <table>
                        <tr>
                            <td>Email của bạn </td>
                            <td><input type="text" name="email_gui" size="30" value="<?php echo $email_gui; ?>"/>*
                                <span class="error"><?php echo $error_email_gui; ?></span>
                            </td>
                        </tr>
                        <tr>
                            <td> Tên của bạn</td>
                            <td> <input type="text" name="ten_gui" size="30" value="<?php echo $ten_gui; ?>"/>*
                                <span class="error"><?php echo $error_ten_gui; ?></span>
                            </td>
                        </tr>
                        <tr>
                            <td> Email người nhận </td>
                            <td><input type="text" name="email_nhan" size="30" value="<?php echo $email_nhan; ?>"/>*
                                <span class="error"><?php echo $error_email_nhan; ?></span>
                            </td>
                        </tr>
                        <tr>
                            <td>Tên người nhận </td>
                            <td> <input type="text" name="ten_nhan" size="30" value="<?php echo $ten_nhan; ?>"/>*
                                <span class="error"><?php echo $error_ten_nhan; ?></span>
                            </td>
                        </tr>
                        <tr>
                            <td> Tiêu đề thiệp </td>
                            <td><input type="text" name="tieu_de" size="30" value="<?php echo $tieu_de; ?>"/>*
                                <span class="error"><?php echo $error_tieu_de; ?></span>
                            </td>
                        </tr>
                    </table><br/>
                    Lời nhắn <br/>
                    <textarea rows="9" cols="70"style="background-color: #BEFFBE" name="loi_nhan"><?php echo $loi_nhan; ?></textarea><br/>
                    <input type="hidden" name="thiepID" value="<?php echo $thiep['thiep_id']; ?>"/>
                    <input type="submit" name="xem_truoc" value="Xem trước"/>
                    <input type="submit" name="gui" value="Gửi ngay"/>
                </form>

            </div>
        </div>
    </div>
    <?php
} else if ($output_form == "no") {
    //thuc hien gui thiep
    $thiep_gui_id = time();
    $query = "INSERT INTO tbl_guithiep VALUES ($thiep_gui_id," . $thiep['thiep_id'] . ",'$tieu_de','" . $thiep['thiep_hinh'] . "','$loi_nhan','$ten_gui','$ten_nhan','$email_gui','$email_nhan')";
    mysqli_query($dbc, $query);

    include 'phpmailer/sendMail.php';
    $body = "Chào $ten_nhan!\n\n" .
            "$ten_gui đã gửi cho bạn một tấm thiệp từ támthángba.vn\n\n" .
            "Để xem thiệp, bạn chỉ cần click vào đường dẫn phía dưới hoặc copy và dán vào thanh địa chỉ của trình duyệt và Enter\n\n" .
            "http://localhost/WomanDay/xemThiep.php?id=$thiep_gui_id\n\n\n" .
            "Thân chào!\n\n" .
            "támthángba.vn";
    @smtpmailer($email_nhan, $body);
    echo 'Thư của bạn đã được gửi đi!';
}

include_once 'footer.inc';
?>
